package Servelt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Connection conn = null;   PreparedStatement preparedStatement=null;   ResultSet rs;

        String username=request.getParameter("username");
        String password=request.getParameter("password");

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn= DriverManager.getConnection("jdbc:mysql://localhost:3307/xiangmu?characterEncoding=utf8","root","1234");
            String sql = "select * from vip where username=?";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,username);
            rs=preparedStatement.executeQuery();
            if(!rs.next())
            {
                response.getWriter().println("用户不存在！");
            }
            else {
                   if (rs.getString("password").equals(password))
                    {response.getWriter().println("登录成功！");}
                    else
                    {response.getWriter().println("密码错误！");}
            }
            preparedStatement.close();	conn.close();
        } catch (Exception e)
        {
            System.out.println(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
